package com.qf.utils;


import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author Mr.Liu
 * @date 2021/11/8 13:16
 */
@SuppressWarnings("all")
public class dbutilUtils {
    private static DataSource dataSource = null;
    private static ThreadLocal<Connection> tl = new ThreadLocal<>();

    static{
        try {
            Properties p = new Properties();
            InputStream input = dbutilUtils.class.getClassLoader().getResourceAsStream("dbutils.properties");
            p.load(input);
            dataSource = DruidDataSourceFactory.createDataSource(p);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource(){
        return dataSource;
    }

    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = tl.get();
            if(connection == null){
                connection = dataSource.getConnection();
                tl.set(connection);
            }
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //封装释放资源代码
    //针对DQL语句操作,释放资源
    //ResuletSet,Statement,Connection
    public static void close(ResultSet rs, Statement stmt, Connection conn){

        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    // 将释放资源也可以封装到功能中
    //针对的是DDL/DML语句操作时释放资源
    public static void close(Statement stmt, Connection conn){
        close(null,stmt,conn);
    }

    //后期需要用到事务
    //封装事务的代码
    //开启事务
    public static void startTransAction(){
        try {
            Connection connection = getConnection();
            connection.setAutoCommit(false);//禁用自动提交
            connection.close();//释放资源,归还到连接池
            tl.remove();//从线程终解绑
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //事务回滚
    public static void rollBackAndClose() throws SQLException {
        Connection connection = getConnection();
        connection.rollback() ;//事务回滚
        //释放资源
        connection.close() ; //归还到连接池中
        //需要从线程中解绑
        tl.remove();
    }

    //提交事务
    public static void commitAndClose() throws SQLException {
        Connection connection = getConnection();
        connection.commit(); ;//提交事务
        //释放资源
        connection.close() ; //归还到连接池中
        //需要从线程中解绑
        tl.remove();
    }

    public static void main(String[] args) {
        /*DataSource dataSource = dbutilUtils.getDataSource();
        System.out.println(dataSource);*/
        Connection connection = dbutilUtils.getConnection();
        System.out.println(connection);
    }
}
